package com.laboratory.controller;

import com.laboratory.domain.ResponseCode;
import com.laboratory.domain.ResponseResult;
import com.laboratory.domain.UserLogin;
import com.laboratory.domain.dto.UserDto;
import com.laboratory.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * BindException （表单参数校验失败）
     */
    @PostMapping(value = "/login", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public ResponseResult loginUser(@Validated @ModelAttribute UserLogin userLogin) {
        return new ResponseResult(
                ResponseCode.LOGIN_SUCCESS,
                "登录成功",
                null
        );
    }

    /**
     * 创建用户接口
     * 使用 @Valid 对请求体中的 UserDto 进行校验
     */
    @PostMapping
    public ResponseResult createUser(@Validated @RequestBody UserDto userDto) {
        /**
         * 如果参数不符合校验规则，会直接抛出异常，被全局异常处理器捕获
         */
        return new ResponseResult(
                ResponseCode.SAVE_SUCCESS,
                "用户创建成功",
                userDto
        );
    }

}
